#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Mar 20 00:26:43 2020

@author: maliaosaide
"""
from model import Model
from data import Data_Preprocessing as Data
from data import Assessment as AS
import numpy as np


def Text_Segmentation(trian_file_name,text_file_name="",sep='\t',tsep=","):
    import jieba
    import pandas as pd
    if text_file_name!="":
        df1 = pd.read_csv(trian_file_name,sep = sep)
        df2 = pd.read_csv(text_file_name,sep =tsep)
        x_train=[' '.join(jieba.cut(i)).split() for i in df1["comment"] ]
        y_train=df1["label"]
    
        x_test=[' '.join(jieba.cut(i)).split() for i in df2["comment"] ]
        y_test=df2["label"]
        return x_train, y_train, x_test, y_test
    else:
        df1 = pd.read_csv(trian_file_name,sep = sep)
        x_train=[' '.join(jieba.cut(i)).split() for i in df1["comment"] ]
        y_train=df1["label"]
        return x_train, y_train,



def train_ML():
    train_x,train_y,test_x,test_y=Text_Segmentation("train1.csv","test.csv",sep='\t',tsep='\t')
    dev_x,dev_y=Text_Segmentation("dev.csv",sep='\t')
    
    train_vec=Data.Word_Embeding(train_x)
    test_vec=Data.Word_Embeding(test_x)
    dev_x=Data.Word_Embeding(dev_x)
    
    
    train_vec = np.array(train_vec)
    test_vec = np.array(test_vec)
    dev_x= np.array(dev_x)
#    
    logisticregression=Model.LogisticRegression(train_vec,train_y)
    AS.KFold_Assessment(logisticregression,train_vec,train_y,"logisticregression")
    
    svm=Model.SVM(train_vec,train_y,kernel="linear")
    AS.KFold_Assessment(svm,train_vec,train_y,"svm")
    
    xgb=Model.XGB(train_vec,train_y)
    AS.KFold_Assessment(xgb,train_vec,train_y,"xgb")
    
    randomforest=Model.RandomForestClassifier(train_vec,train_y)
    AS.KFold_Assessment(randomforest,train_vec,train_y,"randomforest")
    
    bayes=Model.Bayes(train_vec,train_y)
    AS.KFold_Assessment(bayes,train_vec,train_y,"bayes")
    
    per_y=AS.Predict(logisticregression,test_vec)
    score=AS.F1(test_y,per_y)
    print(score)
    
    
def train_DL():
    
    train_x,train_y,test_x,test_y=Text_Segmentation("train1.csv","test.csv",sep='\t',tsep='\t')
    dev_x,dev_y=Text_Segmentation("dev.csv",sep='\t')  
      
    train_vec=Data.Word_Embeding(train_x)
    test_vec=Data.Word_Embeding(test_x)
    dev_x=Data.Word_Embeding(dev_x)
               
    train_vec = np.array(train_vec)
    test_vec = np.array(test_vec)
    dev_x= np.array(dev_x)    
    
    train_vec = np.array(train_vec).reshape(-1,1,300)
    train_y=np.array(train_y).reshape(-1,1,1)
    dev_x= np.array(dev_x).reshape(-1,1,300)
    dev_y= np.array(dev_y).reshape(-1,1,1)
    test_vec= np.array(test_vec).reshape(-1,1,300)
#    print(test_vec.shape)
    history,bgru=Model.BGRU(train_vec,train_y,(1,300),dev_x,dev_y,epochs=20,
                    batch_size=32,optimizer='adam',
               )
    AS.DNN_Loss_and_Accuracy(history)
    
    per_y=bgru.predict_classes(test_vec).reshape(1,1,-1)
    per_y=[i for i in per_y][0][0]
    per_y=list(per_y)
    print(per_y,test_y)
    score=AS.F1(test_y,per_y)
    print(score)
    
def main ():
    train_ML()
    train_DL()
    
if __name__ == "__main__":
    main()
    
    
            
        
    


    